package com;

import java.awt.BorderLayout;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class UI extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton button;
	private JTextArea txt;
	private Socket socket;

	public void init() {
		setSize(600, 800);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		button = new JButton("发送");
		txt = new JTextArea();
		add(button, BorderLayout.SOUTH);
		add(txt, BorderLayout.CENTER);
		setVisible(true);

		button.addActionListener(e -> {
			sendMsg(txt.getText());
		});
	}

	public void connect() {
		String ip = "127.0.0.1";
		int port = 8848;
		try {
			socket = new Socket(ip, port);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void sendMsg(String msg) {
		// 发送消息
		OutputStream out;
		try {
			out = socket.getOutputStream();
			PrintStream printStream = new PrintStream(out);
			printStream.println(msg);
			printStream.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// 接收消息
//		InputStream in = socket.getInputStream();
//		Scanner scanner = new Scanner(in);
//		System.out.println("服务端>" + scanner.nextLine());
	}

	public static void main(String[] args) {
		UI ui=new UI();
		ui.init();
		ui.connect();
	}
}
